library(xtable)

######################################################
######################################################
#### Figures 1 and 2: Imputation
######################################################
######################################################
rm(list=ls())
load("PA_All_Impute" )
load("PA_Leaders_Impute")
load("PA_Random_Impute")

V.all1<-scale.text.all$Vdim1
V.ran1<-scale.text.ran$Vdim1
V.lead1<-scale.text.lead$Vdim1


V.all2<-scale.text.all$Vdim2
V.ran2<-scale.text.ran$Vdim2
V.lead2<-scale.text.lead$Vdim2


rotate<-function(x) sign(cor(x,scale.text.all$Vdim1[,1]))*x
rotate2<-function(x) sign(cor(x,scale.text.all$Vdim2[,1]))*x


V.all1<-rowMeans(apply(V.all1,2,rotate))
V.ran1<-rowMeans(apply(V.ran1,2,rotate))
V.lead1<-rowMeans(apply(V.lead1,2,rotate))

V.all2<-rowMeans(apply(V.all2,2,rotate2))
V.ran2<-rowMeans(apply(V.ran2,2,rotate2))
V.lead2<-rowMeans(apply(V.lead2,2,rotate2))


##Rotate to match published figures
adj.all<-sign(V.all1[1]);V.all1<-V.all1*adj.all
adj.ran<-sign(V.ran1[1]); V.ran1<-V.ran1*adj.ran
adj.lead<-sign(V.lead1[1]); V.lead1<-V.lead1*adj.lead

adj.all<-sign(V.all2[1]);V.all2<-V.all2*adj.all
adj.ran<-sign(V.ran2[1]); V.ran2<-V.ran2*adj.ran
adj.lead<-sign(V.lead2[3]); V.lead2<-V.lead2*adj.lead



which.ran.drop<-c( 6, 20 ,27 ,38 ,57, 59, 63 ,87, 90 ,98)

##Figure 1
pdf("PA_SFA_Supp1.pdf",h=6,w=12)
par(mfcol=c(1,2),mar=c(3,3,1,.4))
plot(-V.all1,-V.ran1,type="n",xlab="",ylab="")
abline(c(0,1))
points(-V.all1[-which.ran.drop],-V.ran1[-which.ran.drop],pch=19,col=gray(.7))
points(-V.all1[which.ran.drop],-V.ran1[which.ran.drop],pch=4,cex=1.4,font=2)
mtext(side=1,"Legislator Score (All Data)",line=2)
mtext(side=2,"Legislator Score (Missing Data)",line=2)
mtext(side=3,"First Dimension",font=2)
legend("topleft",c("Missing Legislators","Observed Legislators"),col=c("black",gray(.7)),pch=c(4,19),bty="n")


plot(V.all2,V.ran2,type="n",xlab="",ylab="")
abline(c(0,1))
points(V.all2[-which.ran.drop],V.ran2[-which.ran.drop],pch=19,col=gray(.7))
points(V.all2[which.ran.drop],V.ran2[which.ran.drop],pch=4,cex=1.4,font=2)
mtext(side=1,"Legislator Score (All Data)",line=2)
mtext(side=2,"Legislator Score (Missing Data)",line=2)
mtext(side=3,"Second Dimension",font=2)
legend("topleft",c("Missing Legislators","Observed Legislators"),col=c("black",gray(.7)),pch=c(4,19),bty="n")
dev.off()

##Figure 2
nom.data<-read.csv("dwnom112.csv")
party<-ifelse(nom.data[,"V6"]==100,"D","R")
lead.names<-tolower(c("REID","SCHUMER","MCCONN","KYL"))
lead.vec<-sapply(lead.names,FUN=function(x) grep(x,nom.data[,4]))


range1<-range(c(V.all1,V.lead2,V.all2,V.lead1))

pdf("PA_SFA_Supp2.pdf",h=6,w=12)
par(mfcol=c(1,2),mar=c(3,3,1,.4))
plot(-V.all1,V.lead2,type="n",xlab="",ylab="",xlim=range1,ylim=range1)
abline(c(0,1))
points(-V.all1[-lead.vec],V.lead2[-lead.vec],pch=party[-lead.vec],col=gray(.7),cex=1.2)
points(-V.all1[lead.vec],V.lead2[lead.vec],pch=party[lead.vec],col=gray(0),cex=1.2)
points(-V.all1[lead.vec],V.lead2[lead.vec],pch=1,col=gray(0),cex=2.5)

mtext(side=1,"First Dimension (All Data)",line=2)
mtext(side=2,"Second Dimension (Imputed)",line=2)
mtext(side=3,"Ideology Dimension",font=2)
legend("topleft",c("Republicans","Democrats"),pch=c("R","D"),bty="n")


plot(-V.all2,V.lead1,type="n",xlab="",ylab="",xlim=range1,ylim=range1)
abline(c(0,1))
points(V.all2[-lead.vec],V.lead1[-lead.vec],pch=party[-lead.vec],col=gray(.7),cex=1.2)
points(V.all2[lead.vec],V.lead1[lead.vec],pch=party[lead.vec],col=gray(0),cex=1.2)
points(V.all2[lead.vec],V.lead1[lead.vec],pch=1,col=gray(0),cex=2.5)

mtext(side=1,"Second Dimension (All Data)",line=2)
mtext(side=2,"First Dimension (Imputed)",line=2)
mtext(side=3,"Leadership Dimension",font=2)
legend("topleft",c("Republicans","Democrats"),pch=c("R","D"),bty="n")

dev.off()

######################################################
######################################################
#### Figure 3: Scaling Media
######################################################
######################################################
rm(list=ls())
load('scale.media')


##Order for media output
# "nyt.row"      "wsj.row"      "washpost.row"

V.media1<-scale.media$Vdim1
rotate<-function(x) sign(cor(x,scale.media$Vdim1[,1]))*x

V1<-rowMeans(apply(V.media1,2,rotate))
nom.data<-read.csv("dwnom112.csv")
isrep<-nom.data[,"V6"]==200
isdem<-nom.data[,"V6"]!=200

med.ests<-V1[1:3]*sign(V1[2])
dim.ests<-V1[-c(1:3)]*sign(V1[2])

d1<-density(dim.ests[isdem],cut=0)
r1<-density(dim.ests[isrep],cut=0)
ylim.use<-c(0,max(c(d1$y,r1$y)))
xlim.use<-range(c(d1$x,r1$x))

pdf("PA_SFA_Supp3.pdf",h=6,w=9)
par(mar=c(3,3,2,.2))
plot(d1,ylim=ylim.use,xlim=xlim.use,col=gray(.7),lwd=2,xlab="",ylab="",main="")
lines(r1,col=gray(.7),lwd=2)
points(dim.ests,0*dim.ests,pch="|",col=gray(.6))
text("Democrats",x=-.75,y=.2,col=gray(.6))
text("Republicams",x=1.25,y=.2,col=gray(.6))
segments(x0=-.75+0*dim.ests[isdem],y1=.05+0*dim.ests[isdem],x1=dim.ests[isdem],y0=.16,col=gray(.6))
segments(x0=1.25+0*dim.ests[isrep],y1=.05+0*dim.ests[isrep],x1=dim.ests[isrep],y0=.16,col=gray(.6))
sapply(med.ests,FUN=function(x) abline(v=x,lwd=2))

text("NYT",x=-1,y=.6,font=2,pos=3)
arrows(x0=-1,y0=.6,x1=med.ests[1],y1=.5,length=.1)

text("WSJ",x=1,y=.6,font=2,pos=3)
arrows(x0=1,y0=.6,x1=med.ests[2],y1=.5,length=.1)

text("Wash Post",x=.7,y=.4,font=2,pos=3)
arrows(x0=.7,y0=.4,x1=med.ests[3],y1=.3,length=.1)
mtext(side=1,"First Scaled Dimension",line=2)
mtext(side=2,"Density",line=2)
mtext(side=3,"Newspapers Placed on the Same Scale as Legislators",line=.3,font=2)
dev.off()
######################################################
######################################################
#### Figure 4: Criterion
######################################################
######################################################
rm(list=ls())
load('criterion')

C[,1]<-1/C[,1]
C1<-C[1:12,1:4]*0

for(i in 1:7){
	C1<-C1+t(apply(C[i*12+1:12,1:4],1,FUN=function(x) x/C[i*12+12,1:4]))/7
}


C2<-C1[11:1,]

pdf("PA_SFA_Supp4.pdf",h=6,w=6)
par(mar=c(3,3,.2,.2))
plot((0:10)/10,(0:10)/10,ylim=range(C2),type="n",xlab="",ylab="")
abline(h=1,lwd=1.2,col=gray(.5))
for(i in 1:4) lines((0:10)/10,C2[1:11,i],lty=i)
mtext(side=1,line=2,expression(paste("Proportion of Information Coming from Votes (",alpha,")")))
mtext(side=2,line=2,"Performance Relative to Null Model (Null = 1)")
legend("topright",bty="n",legend=c("Discrimination","WAIC, Pooled","WAIC, Votes","WAIC, Words"),lty=1:4)
dev.off()


######################################################
######################################################
#### Table 1: Data Summary 
######################################################
######################################################
rm(list=ls())

CongressNum<-105
table.run<-NULL
for(CongressNum in 105:112){


file.read<-paste("dtmbig",CongressNum,sep="_")
load(file=file.read)

file.read<-paste("dtm",CongressNum,sep="_")
load(file=file.read)

file.read<-paste("votemat",CongressNum,sep="_")
load(file=file.read)

row.curr<-c(CongressNum,nrow(votes.mat),ncol(votes.mat),
ncol(dtm.analyze),mean(dtm.analyze==0)*100,
ncol(dtm2),mean(dtm2==0)*100,min(dtm2),max(dtm2))

table.run<-rbind(table.run,row.curr)



}
colnames(table.run)<-c("Senate","Senators","Votes","Number","% Zero", "Number", "% Zero",
	"Minimum","Maximum")
table.run.0<-table.run[,-1]
rownames(table.run.0)<-105:112


digs.use<-rep(0,ncol(table.run.0)+1)
digs.use[c(5,7)]<-1
xtable(table.run.0,dig=digs.use,include.rownames=F)

######################################################
######################################################
#### Remaining Figures: Scaling Words
######################################################
######################################################
rm(list=ls())

## 105th to 112 Congress
sessions <- 105:112

for(s in 1:length(sessions)){
    session <- sessions[s]

    fname1 <- paste("dtm_", session, sep="")
    load(fname1)
    fname2 <- paste("obj_words_", session, sep="")
    load(fname2)
    
    rownames(obj.curr$U.all) <-colnames(dtm2)

    fname3 <- paste("dims", session, ".pdf", sep="")
    fname3 <- paste("PA_SFA_Supp", (5:12)[s], ".pdf", sep="")
    pdf(file= fname3, width=10, height=8)

    step_size <- 3
    distance <- 30
    n_dims <- 6
    n_top <- 10 # 10 words
    n_bottom <- 10 # 10 words
    n_empty <- 3
    n_words <- n_top+n_bottom+n_empty
    ytop <- (n_top+n_bottom+n_empty)*step_size
    ylim <- ytop+10

    par(cex.axis=1)
    plot(rep(1,n_words), seq(from=ytop, to=1, by=-step_size),
         type="n",
         xlim=c(0,distance*n_dims+distance), ylim=c(0,ylim),
         xaxt='n', yaxt='n',
         xlab="", ylab="",
         bty='n')

    for(i in 1:n_dims){
        ## flipping dimension 3 and 5 for just display
        if(i==3){
            idx <- 5
        } else if(i==5){
            idx <- 3
        } else {
            idx <- i
        }

        idx_last10 <- c(dim(obj.curr$U.all)[1]:(dim(obj.curr$U.all)[1]-9))
        
        yloc.i <- seq(from=ytop, to=1, by=-step_size)
        top.cex <- abs(as.numeric(sort(obj.curr$U.all[,idx])[idx_last10]))
        bottom.cex <- abs(as.numeric(sort(obj.curr$U.all[,idx])[n_top:1]))
        if(i==1){
            bottom.cex <- abs(as.numeric(sort(obj.curr$U.all[,idx])[idx_last10]))
            top.cex <- abs(as.numeric(sort(obj.curr$U.all[,idx])[1:n_top]))
        }

        ## normalize font size
        size.cex <- c(top.cex,rep(5,n_empty), bottom.cex)
        size.cex_norm <- (sqrt(size.cex)/max(sqrt(size.cex)))
        size.cex_norm <- (2*size.cex_norm)^(1/3)
        
        bottom.words <- names(sort(obj.curr$U.all[,idx])[n_top:1])
        top.words <- names(sort(obj.curr$U.all[,idx])[idx_last10])
        if(i==1){
            idx_last10 <- c((dim(obj.curr$U.all)[1]-9):(dim(obj.curr$U.all)[1]))
            top.words <- names(sort(obj.curr$U.all[,idx])[1:n_top])
            bottom.words <- names(sort(obj.curr$U.all[,idx])[idx_last10])
        }
        empty.words <- rep(".",n_empty)

        text(rep(distance*i,n_words), yloc.i,
             c(top.words,empty.words,bottom.words),
             cex=size.cex_norm)
        
        text(distance*i+1,ylim-4, paste("Dimension ", i, sep=""),
             cex=1.1) # distance to 160
        rect(distance*i-12,ylim-7,distance*i+12,ylim-6,
             col = rgb(0.5,0.5,0.5,1/4), border=NA) #xleft,ybottom, xtop, ytop
    }

    axis(c("Words with Negative Level", "Words with Positive Level"),
         side=2,
         at=c(16,ylim/2+16), tick=FALSE, line=-4)

    arrows(10,1,10,ylim/2-8, code=1,
           length=.1)

    arrows(10,ylim/2+1,10,ylim-8, code=2,
           length=.1)

    dev.off()

}



